package com.turing.post.cardmanagement.util;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import com.turing.post.util.PCPSUtil;

@SuppressWarnings("serial")
public class ApduExecuteObj implements Serializable {
	/**
	 * appId
	 */
	private BigInteger appId = BigInteger.ZERO;

	public void setAppId(BigInteger appId) {
		this.appId = appId;
	}

	public BigInteger getAppId() {
		return appId;
	}

	/**
	 * 圈存流水号
	 */
	private String loadSeqNo = "";

	public void setLoadSeqNo(String loadSeqNo) {
		this.loadSeqNo = loadSeqNo;
	}

	public String getLoadSeqNo() {
		return loadSeqNo;
	}

	/**
	 * APDU列表
	 */
	private List<CardApdu> apduList = new ArrayList<CardApdu>();

	public void setApduList(List<CardApdu> apduList) {
		this.apduList = apduList;
	}

	public List<CardApdu> getApduList() {
		return apduList;
	}
	
	/**
	 * APDU指令
	 */
	private String apduString = "";
	
	public String getApduString() {
		return apduString;
	}

	public void setApduString(String apduString) {
		this.apduString = apduString;
	}

	/**
	 * 编码APDU指令,将数组转换成字符串指令
	 * @param obj
	 * @return
	 */
	public static String codingApduExecuteObj(ApduExecuteObj obj) {
		String apduString = null;
		if (obj == null || obj.getApduList() == null
				|| obj.getApduList().size() == 0) {
			return apduString;
		}
		CardApdu cardApdu = null;
		apduString = obj.getApduList().size() + ";";
		List<CardApdu> apduList = obj.getApduList();
		for (int i = 0; i < apduList.size(); i++) {
			cardApdu = apduList.get(i);
			apduString = apduString + cardApdu.getApdu() + "_"
					+ cardApdu.getApduCategory() + ";";
		}
		return apduString;
	}
	
	/**
	 * 解析APDU指令,将指令转换成数组
	 * @param apduString
	 * @return
	 */
	public static ApduExecuteObj resolveApduExecuteObj(String apduString) {
		if(apduString == null || PCPSUtil.isEmpty(apduString)){
			return null;
		}
		List<CardApdu> apduList = new ArrayList<CardApdu>();
		String[] apduStr = apduString.split(";");
		CardApdu cardApdu = null;
		for (int i = 1; i < apduStr.length; i++) {
			cardApdu = new CardApdu();
			String[] aud = apduStr[i].split("_");
			cardApdu.setApduResult(aud[0]);
			cardApdu.setApduCategory(Integer.parseInt(aud[1]));
			apduList.add(cardApdu);
		}
		ApduExecuteObj aeo = new ApduExecuteObj();
		aeo.setApduList(apduList);
		return aeo;
	}
	
}
